package com.dandan.mobilelover.component;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
public class Pagination {
		private Integer page = 1; // 当前页
		private Integer pageSize = 10; // 一页多少
		private Integer prePage = 1; // 上一页
		private Integer nextPage = 2; // 下一页
		private Integer startRow = 1; // 开始位置
		private Integer maxPage = Integer.MAX_VALUE; //最大页数
		private String html; //返回分页内容
		private int viewPage = 5;
		private String jsFunction;
	/*
	 * js回调函数，
	 */
		public String getJsFunction() {
			return jsFunction;
		}

		public void setJsFunction(String jsFunction) {
			this.jsFunction = jsFunction;
		}

		public int getViewPage() {
			return viewPage;
		}

		public void setViewPage(int viewPage) {
			this.viewPage = viewPage;
		}

		public void setHtml(String html) {
			this.html = html;
		}

		public String getHtml() {
			String temp = "<div  class='pagelinks' >";
			if (maxPage > 1) {
				prePage = this.page == 1 ? 1 : this.page - 1;
				temp += "<a href='#' onclick='"+ this.jsFunction +"(1)'>首页</a>";
				temp += "<a href='#' onclick='"+ this.jsFunction +"("
						+ this.prePage + ")'>上一页</a>";
				int start = this.page - this.viewPage / 2;
				int end = this.page + this.viewPage / 2;
				if (page < 4) {
					start = 1;
					end = 5;
				}

				if (maxPage < end) {
					start = maxPage - 4;
					end = maxPage;
					if (start < 1) {
						start = 1;
					}
				}

				for (int i = start; i <= end; i++) {
					if (page == i) {
						temp += "<strong class='current'>" + i + "</strong><//a>";
					} else {
						temp += "<a href='#' onclick='"+ this.jsFunction +"(" + i
								+ ")'>" + i + "</a>";
					}
				}

				temp += "<a href='#' onclick='"+ this.jsFunction +"(" + nextPage
						+ ")'>下一页</a>";
				temp += "<a href='#' onclick='"+ this.jsFunction +"(" + maxPage
						+ ")'>尾页</a>";
				System.out.println(temp);
			}
			temp += "</div>";
			this.html = temp;
			return html;
		}

		public int getPage() {
			return page;
		}

		public void setCurrentPage(int page) {
			this.page = page;
			if (this.page <= 0)
				this.page = 1;
			if (this.page > maxPage)
				this.page = maxPage;

			// prePage, nextPage, startRow Ҫ�� page �ı�
			prePage = this.page == 1 ? 1 : this.page - 1;
			nextPage = this.page == maxPage ? maxPage : this.page + 1;
			startRow = (this.page - 1) * pageSize + 1;
		}

		public void setTotalCount(int count) {
			maxPage = (int) 
			        (count%pageSize==0?
			        (count/pageSize):
			        (count/pageSize+1));
			if (this.maxPage <= 0)
				this.maxPage = 1;

			// nextPage Ҫ�� maxPage �ı�
			nextPage = this.page == maxPage ? maxPage : this.page + 1;
			startRow = this.page > maxPage ? ((maxPage - 1) * pageSize + 1)
					: (this.page - 1) * pageSize + 1;
			if(this.nextPage > this.maxPage){
				this.nextPage = this.maxPage;
			}
		}

		public Integer getPageSize() {
			return pageSize;
		}

		public void setPageSize(Integer pageSize) {
			this.pageSize = pageSize;
		}

		public Integer getPrePage() {
			return prePage;
		}

		public void setPrePage(Integer prePage) {
			this.prePage = prePage;
		}

		public Integer getNextPage() {
			return nextPage;
		}

		public void setNextPage(Integer nextPage) {
			this.nextPage = nextPage;
		}

		public Integer getStartRow() {
			return startRow;
		}

		public void setStartRow(Integer startRow) {
			this.startRow = startRow;
		}

		public Integer getMaxPage() {
			return maxPage;
		}

		public void setMaxPage(Integer maxPage) {
			this.maxPage = maxPage;
		}

		public void setPage(Integer page) {
			this.page = page;
		}
}
